package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.example.hospital.api.common.PageUtils;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.form.AddNumByIdForm;
import com.example.hospital.api.controller.form.SearchDrugNumByPageForm;
import com.example.hospital.api.controller.form.SerchDrugInfByIdForm;
import com.example.hospital.api.service.DrugNumService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/drugnum")
public class DrugNumContriller {
    @Resource
    private DrugNumService drugService;

    @PostMapping("/searchNumByPage")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public R searchNumByPage(@RequestBody @Valid SearchDrugNumByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = drugService.searchNumByPage(param);
        return R.ok().put("result", pageUtils);
    }

    @PostMapping("/addNumById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public R addNumById(@RequestBody @Valid AddNumByIdForm form) {
        Map param = BeanUtil.beanToMap(form);
        drugService.addNumById(param);
        return R.ok();
    }

    @PostMapping("/reduceNumById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public R reduceNumById(@RequestBody @Valid AddNumByIdForm form) {
        Map param = BeanUtil.beanToMap(form);
        String result=drugService.reduceNumById(param);
        return R.ok().put("result", result);
    }

    @PostMapping("/searchById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public R searchById(@RequestBody @Valid SerchDrugInfByIdForm form) {
        HashMap map = drugService.searchById(form.getId());
        return R.ok(map);
    }
}
